using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Resources;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Drawing.Imaging;
using System.Reflection;


namespace MultilanguageControls
{
    /// <summary>
    /// HTTP handler that obtains the image from de resource file
    /// </summary>
    /// <remarks>
    /// It's nedded to register this HTTP handler in the web.config file of the web application as showed
    ///     <example>
    ///         
    ///         <system.web>
    ///             <httpHandlers>
    ///			        <add verb="*" path="MultilanguageControlsHandler.ashx" type="MultilanguageControls.Handler, MultilanguageControls" />
    ///		        </httpHandlers>
    ///         </system.web>
    /// 
    ///     </example>
    /// </remarks>
    public class Handler : IHttpHandler
    {
        private string _culture, _resourceType, _key, _format;
        private MemoryStream _imageStream;

        /// <summary>
        /// Implements the IHttpHandler.ProcessRequest
        /// </summary>
        /// <param name="context">The current execution HTTP process</param>
        public void ProcessRequest(HttpContext context)
        {
            GetParameters(context);
            SaveImage();

            //Return the image to the control
            context.Response.ContentType = string.Format("image/{0}", _format.ToString());
            context.Response.BinaryWrite(_imageStream.GetBuffer());
        }

        /// <summary>
        /// Obtain parameters passed by the image control
        /// </summary>
        private void GetParameters(HttpContext context)
        {
            _culture = context.Request.QueryString["culture"];
            _resourceType = context.Request.QueryString["resource"];
            _key = context.Request.QueryString["key"];
            _format = context.Request.QueryString["format"];
        }

        /// <summary>
        /// Get the image from the resource file and save it on a memory stream
        /// </summary>
        private void SaveImage()
        {
            //Get the image from de resource file
            ResourceManager rm = new ResourceManager(_resourceType, GetResourceAssembly());
            Bitmap bmp = (Bitmap)rm.GetObject(_key, new CultureInfo(_culture));

            //Save the image in a memory stream
            _imageStream = new MemoryStream();
            bmp.Save(_imageStream, GetImageFormat());
        }

        /// <summary>
        /// Implements the IHttpHandler.IsReusable
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Find and return the satelite assembly with the resources
        /// </summary>
        /// <returns>System.Reflection.Assembly object</returns>
        private Assembly GetResourceAssembly()
        {
            Assembly assembly = null;

            foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (a.FullName.IndexOf("App_GlobalResources") > -1)
                {
                    assembly = a;
                    break;
                }
            }

            return assembly;
        }

        /// <summary>
        /// Find the specified image format in the control's ImageFormat property
        /// </summary>
        /// <returns>ImageFormat object</returns>
        private ImageFormat GetImageFormat()
        {
            ImageFormat imgFormat = ImageFormat.Jpeg;

            foreach (PropertyInfo p in typeof(ImageFormat).GetProperties())
            {
                if (p.Name == _format)
                {
                    imgFormat = (ImageFormat)p.GetValue(null, null);
                    break;
                }
            }

            return imgFormat;
        }
    }
}
